//
//  ProductSKUView.m
//  BaseProject
//
//  Created by 王洋 on 2020/8/4.
//  Copyright © 2020 王洋. All rights reserved.
//
#import "ZZLayout.h"

#import "SearchSectionHeader.h"

#import "ProductSKUCell.h"

#import "ProductSKUView.h"

@interface ProductSKUView ()<UICollectionViewDelegate, UICollectionViewDataSource, ZZLayoutDelegate, UITextFieldDelegate>

@end

@implementation ProductSKUView


- (void)awakeFromNib {
    [super awakeFromNib];

    [self layout];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.01 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.handleBtn pp_clipCorners:UIRectCornerAllCorners radius:self.handleBtn.frame.size.height/2];
        
        [self pp_clipCorners:UIRectCornerTopLeft|UIRectCornerTopRight radius:10];
        [self.skuImageView pp_clipCorners:UIRectCornerAllCorners radius:8];
    });
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
    self.selectModel.goodsNum = textField.text.integerValue;
}


- (void)fillSkuViewWithArray:(NSMutableArray <CommonSkuModel *>*)dataArray Type:(ProductSKUViewActionType)type {
    self.type = type;
    self.dataArray = dataArray;
    
    if (dataArray.count > 0) {
        [self fillViewWithModel:self.dataArray.firstObject];
    }
    [self.collectionview reloadData];
}

- (void)fillViewWithModel:(CommonSkuModel *)model {
    
    self.selectModel = model;
    
    [self.skuImageView sd_setImageWithURL:[NSURL URLWithString:model.specImage] placeholderImage:[UIImage imageNamed:@"placeholder"]];
    
//    if ([TTUserModel sharedObject].token.length > 0) {
        self.skuPriceLab.text =[NSString stringWithFormat:@"¥ %@",[Utilities changeDoubleToString:model.specPrice]];
//    } else {
//        self.skuPriceLab.text = @"登录后查看补贴价";
//    }

    self.skuStockLab.text = [NSString stringWithFormat:@"库存：%ld",(long)model.specNum];
    self.selSKULab.text = [NSString stringWithFormat:@"已选规格：%@",model.specName];
    
    if (model.specNum > 0) {
        self.selectModel.goodsNum = 1;
    } else {
        self.selectModel.goodsNum = 0;
    }
    
    self.countTF.text = [NSString stringWithFormat:@"%ld",(long)self.selectModel.goodsNum];
}


- (IBAction)addAction:(id)sender {
    
    if (self.selectModel.goodsNum < self.selectModel.specNum) {
        self.selectModel.goodsNum++;
        self.countTF.text = [NSString stringWithFormat:@"%ld",(long)self.selectModel.goodsNum];

    } else {
        [SVProgressHUD showErrorWithStatus:@"数量不可以大于库存"];
        [SVProgressHUD dismissWithDelay:1];
    }
}

- (IBAction)cutAction:(id)sender {
    if (self.selectModel.goodsNum > 1) {
        self.selectModel.goodsNum--;
        self.countTF.text = [NSString stringWithFormat:@"%ld",(long)self.selectModel.goodsNum];

    } else {
        [SVProgressHUD showErrorWithStatus:@"数量最少为0"];
        [SVProgressHUD dismissWithDelay:1];
    }
}

- (IBAction)handleAction:(id)sender {
    
    [self endEditing:YES];
    
    if (self.selectModel.goodsNum > 0) {
        if (self.ProductSKUViewBlock) {
            self.ProductSKUViewBlock(self.selectModel, self.type);
        }
    }

}


- (void)layout {
    
    self.countTF.delegate = self;
    
    ZZLayout *layout = [[ZZLayout alloc] initWith:ZZLayoutFlowTypeAutomateFloat delegate:self];

    _collectionview.collectionViewLayout = layout;
    _collectionview.delegate = self;
    _collectionview.dataSource = self;
    
    //注册cell
    [_collectionview registerNib:[UINib nibWithNibName:@"ProductSKUCell" bundle:nil] forCellWithReuseIdentifier:@"ProductSKUCellID"];
    
    [_collectionview registerNib:[UINib nibWithNibName:@"SearchSectionHeader" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"SearchSectionHeader"];
    
    
    [_collectionview registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"UICollectionElementKindSectionFooter"];
    
    _collectionview.showsHorizontalScrollIndicator = NO;
    _collectionview.bounces = YES;

    if (@available(iOS 11.0, *)) {
        _collectionview.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    _collectionview.backgroundColor = [UIColor whiteColor];

}

#pragma mark- 协议方法
//collectionView的协议方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    for (CommonSkuModel *model in self.dataArray) {
        model.isSelect = NO;
    }
    self.dataArray[indexPath.row].isSelect = YES;
    [self fillViewWithModel:self.dataArray[indexPath.row]];
    self.selectModel = self.dataArray[indexPath.row];
    
    
    [self.skuImageView sd_setImageWithURL:[NSURL URLWithString:self.selectModel.specImage] placeholderImage:[UIImage imageNamed:@"placeholder"]];

    [self.collectionview reloadData];
    
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    ProductSKUCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ProductSKUCellID" forIndexPath:indexPath];
    [cell fillCellWithModel:self.dataArray[indexPath.row]];
    return cell;
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    
    UICollectionReusableView *reusableView = nil;
    // 区头
    if (kind == UICollectionElementKindSectionHeader) {
        SearchSectionHeader *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"SearchSectionHeader" forIndexPath:indexPath];
        
        headerView.sectionTitle.text = @"规格";
        headerView.sectionBtn.hidden = YES;
        
        reusableView = headerView;
    }
    // 区尾
    if (kind == UICollectionElementKindSectionFooter) {
        UICollectionReusableView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"UICollectionElementKindSectionFooter" forIndexPath:indexPath];
        reusableView = footerView;
    }
    return reusableView;
}

//ZZLyout的流协议方法
- (CGFloat)layout:(ZZLayout *)layout heightForRowAtIndexPath:(NSIndexPath *)indexPath {//返回item的度
    return 35;//同一section下请不要改变高度.
}

- (CGFloat)layout:(ZZLayout *)layout widthForRowAtIndexPath:(NSIndexPath *)indexPath {//返回item的宽
    
//    NSArray *data = _dataSource[indexPath.section][@"value"];

    return [Utilities getStringWidthWithString:self.dataArray[indexPath.row].specName fontSize:12 andHeight:35] + 20;//这里可以根据内容传入任意宽度
//    return 100;
    
}

- (UIEdgeInsets)layout:(ZZLayout *)layout insetForSectionAtIndex:(NSInteger)section {//设置每个区的边距
    return UIEdgeInsetsMake(10, 15, 10, 15);
}

- (NSInteger)layout:(ZZLayout *)layout lineSpacingForSectionAtIndex:(NSInteger)section {//设置每个区的行间距
    return 10;
}

- (CGFloat) layout:(ZZLayout *)layout interitemSpacingForSectionAtIndex:(NSInteger)section {//设置每个区的列间距
    return 15;
}

- (CGSize)layout:(ZZLayout *)layout referenceSizeForHeaderInSection:(NSInteger)section {//设置区头的高度
    return CGSizeMake(TTScreenWidth, 44);
}

- (UIColor *)layout:(UICollectionView *)layout colorForSection:(NSInteger)section {
    return [UIColor whiteColor];
}

- (CommonSkuModel *)selectModel {
    if (!_selectModel) {
        _selectModel = [[CommonSkuModel alloc] init];
    }
    return _selectModel;
}

@end
